﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using yaip;
using yaip.Filters.PixelPoint;
using yaip.Filters.PixelGroup;
using yaip.Filters;
using yaip.Statistics;

namespace yaipApp {
    public partial class yaipApp : Form {


        private string fileName;
        private Bitmap imageBitmap;


        public yaipApp() {
            InitializeComponent();
            imageBitmap = new Bitmap(2, 2);
        }

        private void openImageMenuItem_Click(object sender, EventArgs e) {

            if (openFileDialog.ShowDialog() == DialogResult.OK) {
                fileName = openFileDialog.FileName;
                imageBitmap = (Bitmap)Bitmap.FromFile(fileName);
                imageBox.Image = imageBitmap;
            }
        }

        private void exitMenuItem_Click(object sender, EventArgs e) {
            Application.Exit();
        }

        private void invertFilterMenuItem_Click(object sender, EventArgs e) {
            PixelPointFilter invert = new Invert();
            invert.apply(imageBitmap);
            imageBox.Image = imageBitmap;
        }

        private void yaipApp_Resize(object sender, EventArgs e) {
            if (this.Width > imageBitmap.Width || this.Height > imageBitmap.Height) {
                imageBox.SizeMode = PictureBoxSizeMode.Normal;
            } else {
                imageBox.SizeMode = PictureBoxSizeMode.StretchImage;
            }
        }

        private void sharpenFilterMenuItem_Click(object sender, EventArgs e) {
            ConvolutionFilter sharpen = new Sharpen();
            if (sharpen.apply(imageBitmap))
                imageBox.Image = imageBitmap;
        }

        private void gaussianBlurFilterMenuItem_Click(object sender, EventArgs e) {
            ConvolutionFilter gaussian = new GaussianBlur();
            if (gaussian.apply(imageBitmap))
                imageBox.Image = imageBitmap;
        }

        private void grayScaleFilterMenuItem_Click(object sender, EventArgs e) {
            PixelPointFilter grayScale = new GrayScale();
            if (grayScale.apply(imageBitmap))
                imageBox.Image = imageBitmap;

        }

        private void histogramToolStripMenuItem_Click(object sender, EventArgs e) {
            Histogram hist = new Histogram(imageBitmap);
            StringBuilder sb = new StringBuilder();
            foreach (double i in hist.getRedHistogram()) {
                sb.Append(i + " | ");
            }
            MessageBox.Show("Red Mean: " + hist.getRedHistogramMean() +
                "\nGreen Mean: " + hist.getGreenHistogramMean() +
                "\nBlue Mean: " + hist.getBlueHistogramMean());
            
        }
    }
}
